#!/bin/bash
errors='';
#MAKE SURE MYSQL IS INSTALLED AND THAT THE REQUIRED DATABASE IS PRESENT
if !( which mysql ); then
{
echo "No mysql installed here. Would you like me to install it? (Y or N)";
read response;
	if [ $response == "Y" ]; then
		{
		bash sys_resources/mysql.install;
		wait;
		}
	else
		{
		echo "Nothing left to do;"
		exit;
		}
	fi	
}
else
{
	aminstalled=`mysql -u root --password=carblogger -e "DESCRIBE cl_ingests;" carblogger | wc -l`
	if [ $aminstalled -lt 3 ]; then
		{
		echo "Hmm...lights are on but no-one is home. Want me to set up a carblogger database for you?";
		read response;
		if [ $response=="Y" ]; then
			{
				echo "Installing the ingest tables";
				mysql -u root --password=carblogger < sys_resources/alldb_backup.sql				
			}
		fi
		}
	fi
}
fi

#Accept the two digit hour, day, month, and four digit year to operate on.
if [ $# -lt 4 ]; then
	{
		errors="You have fewer than four arguments.";
		errors="$errors You must pass [2 digit hour] [2 digit day] [2 digit month] [4 digit year] seperated by spaces.";
	}
fi 
#SET UP A VARIABLE DETERMINING THE LENGTH OF THE ERRORS MESSAGE
error_length=`echo $errors | wc -c`;
#create an array of active instruments
actives=`cat Config/config | grep active_inst= | sed 's/"//g' | sed 's/active_inst=/ /g'`
#obtain the saroad number
saroad=`cat Config/config | grep saroad= | sed 's/"//g' | sed 's/saroad=/ /g' | tr -d ' '`
#determine the name of the file to parse
file="$saroad-$4-$2-$3";
#TRANSLATE the active instruments array into an array of data value indicators
calused=''
indics=''
indicator=''
for i in $actives
do
	if (stat resources/$i/\.display &> /dev/null); then
		{
			indicator=`(sed 's/\n//g')<resources/$i/\.display`
			echo "this indicator is $indicator ";
			if [ "$indics" == "" ]; then
				{
				
					indics="$indicator";
				
				}
			else
				{
				
					indics="$indics $indicator";
				
				}
			fi
		}
	else
		{
	
			#Modified to allow instruments add virtual cal channels
			if [ "$i" == "environics9100_drv" ] || [[ "$i" == *IZS* ]]; then
			{
				calused='1'
			}
			fi
			#WHAT SHOULD HAPPEN IF NO DISPLAY FILE IS PRESENT FOR THE CURRENT DRIVER
			sleep 2
		}
	fi

done

if [ $error_length -gt 1 ]; then
	{
		echo $errors;
		exit;
	}
fi


#NOW WE NEED TO INGEST AN HOURS DATA FOR EACH OF THE INDICATORS
#THE INGEST SHOULD BE INDEPENDENT OF SAMPLING FREQUENCY
full_file="Data/$file";
tmpfile=.${$}prcssng 
echo 'use carblogger;' > $tmpfile
#counter=0
for i in $indics; 
do
{
	#create temporary process file
	(grep $i | grep _$1 > .tmpprcsfile${$})<$full_file ;
		
		(while read line; do
			is_disabled=0
			#counter=$counter++
			this_entry=`echo $line | sed 's/:::/ /g' | sed 's/::/ /g'`
			#if the string --DISABLED-- appears at the end of a record
			# remove it for the purpose of the ingest and set the
			# is disabled flag to on
			if [ `echo $this_entry | grep -o '\-\-DISABLED\-\-'` > 0 ]; then
			{
		
				this_entry=`echo $this_entry | sed 's/\-\-DISABLED\-\-/ /g'`
				is_disabled=1
			
			}
			fi
			#EXTRACT THE DATE STAMP FROM EACH LINE 
			thisdate=`echo $this_entry | grep -o [0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]_ | sed 's/_//g'`
			#EXTRACT THE TIME STAMP FROM EACH LINE
			thistime=`echo $this_entry | grep -o _[0-9][0-9]:[0-9][0-9] | sed 's/_//g' | sed 's/$/:00/g'`
			#EXTRACT THE NUMERICAL VALUE TO BE STORED IN THE DATABASE
			#USING THE RECOGNITION OF THE [PATTERN=][VALUE] THEN REMOVE THE PATTERN
			thisvalue=`echo $this_entry | grep -o ${i}[-.0-9]* | sed 's/'$i'//g'` 
			#IF THE DATA HAS BEEN PRODUCED WHILE/AFTER DISCONNECTED THEN SET VALUE TO ND
			if [[ -z $thisvalue ]]; then
				{
				 #INDICATE NO VALUE
			 	thisvalue="NV"
				}
			else
			{
			#FOR EACH LINE, CREATE AN ENTRY TO INSERT INTO THE DATABASE, AND TRIM
			#WHITESPACE FROM THE VALUE
			echo "INSERT INTO cl_ingests (value, LOG_TIME, is_offline, data_label) VALUES (trim(\"$thisvalue\"), str_to_date('$thisdate $thistime','%m-%d-%Y %T'), \"$is_disabled\", \"$i\");">>$tmpfile;
                        }
                        fi 
		done)<.tmpprcsfile${$}
rm .tmpprcsfile${$}
}
done 
echo "quit ;" >> $tmpfile
PASS=carblogger
#Show how many records needed ingesting
#echo "$counter lines to enter";
#Determine the number of records in the database prior to the operation
#BEFORE=`/usr/bin/mysql -u root --password=$PASS << EOF
#USE carblogger;
#SELECT COUNT(*) FROM cl_ingests;
#EOF
#`

RESULT=`/usr/bin/mysql -u root --password=$PASS --force  < $tmpfile`
rm $tmpfile;
#DEBUG-----
if [ "$5" = "DEBUG" ];then
	{
		cat $tmpfile;
		echo $calused;
		echo $1;
		echo $2;
		echo $3;
		echo $4;
		echo $indics;
		echo $saroad;
		if [ -e "Data/$file" ]; then
		{
		 	echo "$file found";
		}
		else
		{
			echo "$file not found";
		}
		fi
	}
fi
exit 